//
//  HWTakeALookCommentDetailCell.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/5.
//

import UIKit

class HWTakeALookCommentDetailCell: UITableViewCell {
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabel:GranibleLabel!
    var contentLabel:UILabel!
    var timeLabel:UILabel!
    var zanButton:UIButton!
    var zanLabel:UILabel!
    var superModel:HWTakeALookCommentModel?
    var setZan:((_:String)->())?
    var model:HWTakeALookCommentModel?{
        didSet{
            headImageView.sd_setImage(with: URL(string: model?.face ?? ""), completed: nil)
            nameLabel.text = model?.pet_name
            timeLabel.text = model?.time
//            if self.superModel != nil {
                let replyStr = model?.huifu ?? ""

                let attriString = NSMutableAttributedString.init(string: replyStr + (model?.content ?? ""))
                attriString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 13), range: NSMakeRange(0, replyStr.count))
                attriString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 13), range: NSMakeRange(replyStr.count, attriString.length-replyStr.count))

                contentLabel.attributedText = attriString
//            }else{
//            contentLabel.text = (model?.huifu ?? "") +  (model?.content ?? "")
//            }
            
            zanLabel.text = model?.zan
            if model?.is_zan == true {
                zanButton.setImage(UIImage(named: "commentZanSelect"), for: UIControl.State.normal)
            }else{
                zanButton.setImage(UIImage(named: "commentZanNormal"), for: UIControl.State.normal)
            }
            
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
            if model?.head_frame.count ?? 0 > 0{
                headImageView.snp.remakeConstraints { make in
                    make.left.equalTo(25)
                    make.top.equalTo(20)
                    make.width.height.equalTo(40)
                }
                nameLabel.snp.remakeConstraints { make in
                    make.left.equalTo(headFrameImageView.snp.right).offset(14)
                    make.top.equalTo(headImageView)
                }


            }else{
                headImageView.snp.remakeConstraints { make in
                    make.top.equalTo(10)
                    make.left.equalTo(15)
                    make.width.height.equalTo(40)
                }
                
                nameLabel.snp.makeConstraints { make in
                    make.left.equalTo(headImageView.snp.right).offset(14)
                    make.top.equalTo(headImageView)
                }
            }
            
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#666666"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#666666")]
            }
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWTakeALookCommentDetailCell) {
        let reUse:String = "HWTakeALookCommentDetailCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWTakeALookCommentDetailCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWTakeALookCommentDetailCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWTakeALookCommentDetailCell{
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 20
        headImageView.layer.masksToBounds = true
        contentView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        contentView.addSubview(headFrameImageView)
        
        nameLabel = GranibleLabel()
        nameLabel.font = .systemFont(ofSize: 14)
        contentView.addSubview(nameLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13)
        contentView.addSubview(contentLabel)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#999999", font: 10)
        contentView.addSubview(timeLabel)
        
        zanButton = UIButton.init(type: UIButton.ButtonType.custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: UIControl.Event.touchUpInside)
        zanButton.setImage(UIImage(named: "commentZanNormal"), for: UIControl.State.normal)
        zanButton.isUserInteractionEnabled = true
        contentView.addSubview(zanButton)
        
        zanLabel = HWLabel.createLabel(title: "", color: "#999999", font: 10)
        contentView.addSubview(zanLabel)
        
    }
    
    func initFrame() -> Void {
        headImageView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(15)
            make.width.height.equalTo(40)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.height.width.equalTo(54)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(14)
            make.top.equalTo(headImageView)
        }
        
        zanButton.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.top.equalTo(contentLabel)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.centerX.equalTo(zanButton)
            make.top.equalTo(zanButton.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(headImageView.snp.bottom).offset(-10)
            make.right.equalTo(zanButton.snp.left).offset(-30)
//            make.bottom.equalTo(-10)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(5)
            make.bottom.equalTo(-20)
        }
        
        zanButton.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
        zanButton.setContentCompressionResistancePriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
    }
}
extension HWTakeALookCommentDetailCell{
    @objc func zanButtonClick(_ button:UIButton) -> Void {
        self.setZan!(model?.id ?? "")
    }
}
